﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.POI;

namespace HarmoniousVigor
{
    public partial class Paladin : CharacterClass
    {
        public static PaladinSettings PaladinSettings
        {
            get
            {
                Utils.nullCheckCharacterSettings();
                return (PaladinSettings)HarmoniousVigor.CharacterSettings;
            }
        }
        public string AuraToUse
        {
            get
            {
                Utils.nullCheckCharacterSettings();
                return PaladinSettings.auraToUse;
            }
        }
        //Devotion Aura, Retribution Aura, Concentration Aura, Resistance Aura, Crusader Aura
        public string SealToUse
        {
            get
            {
                Utils.nullCheckCharacterSettings();
                return PaladinSettings.sealToUse;
            }
        }
        //Seal of Insight, Seal of Justice, Seal of Righteousness, Seal of Truth
        public bool HasFullHolyPower 
        { 
            get 
            { 
                if (characterPlayer.HolyPowerPercent == 100) 
                { 
                    return true; 
                } 
                else if (characterPlayer.HasAura("Divine Purpose") && PaladinSettings.divinePurposeCountAsMaxHolyPower) 
                { 
                    return true; 
                } 
                else 
                { 
                    return false; 
                } 
            } 
        }
        public string[] talentTrees = { "Lowbie", "Holy", "Protection", "Retribution" };
        public int judgementRange = 10;
        public bool canCleanseRoot = false;
        private WoWUnit RepentanceTarget;
        public bool classBuff()
        {
            string AuraTemp = "";
            string SealTemp = "";
            if (AuraToUse.Equals("Auto", StringComparison.OrdinalIgnoreCase))
            {
                if (characterPlayer.Mounted)
                {
                    AuraTemp = "Crusader Aura";
                }
                else if (getCurrentSpec() == "Retribution")
                {
                    AuraTemp = "Retribution Aura";
                }
                else if (getCurrentSpec() == "Holy")
                {
                    AuraTemp = "Concentration Aura";
                }
                else if (getCurrentSpec() == "Protection")
                {
                    AuraTemp = "Devotion Aura";
                }
            }
            else
            {
                AuraTemp = AuraToUse;
            }

            if (!characterPlayer.HasAura(AuraTemp) && !SpellManager.GlobalCooldown)
            {
                if (Utils.BuffMe(AuraTemp))
                    return true;
            }

            if (SealToUse == "Auto")
            {
                if ((getCurrentSpec() == "Retribution" && characterPlayer.Level >= 44) || getCurrentSpec() == "Protection")
                {
                    SealTemp = "Seal of Truth";
                }
                else if (getCurrentSpec() == "Retribution" && characterPlayer.Level < 44)
                {
                    SealTemp = "Seal of Righteousness";
                }
                else if (getCurrentSpec() == "Holy")
                {
                    SealTemp = "Seal of Insight";
                }
            }
            else
            {
                SealTemp = SealToUse;
            }

            if (!characterPlayer.HasAura(SealTemp) && !SpellManager.GlobalCooldown && !characterPlayer.Mounted)
            {
                if (Utils.BuffMe(SealTemp))
                    return true;
            }

            if (!characterPlayer.Mounted && !SpellManager.GlobalCooldown)
            {
                if (characterPlayer.HasAura("Mark of the Wild") && !characterPlayer.HasAura("Blessing of Might"))
                {
                    if (Utils.BuffMe("Blessing of Might"))
                        return true;
                }
                else if (characterPlayer.HasAura("Blessing of Kings") && characterPlayer.Auras["Blessing of Kings"].CreatorGuid != characterPlayer.Guid && !characterPlayer.HasAura("Blessing of Might"))
                {
                    if (Utils.BuffMe("Blessing of Might"))
                        return true;
                }
                else if(!characterPlayer.HasAura("Blessing of Kings"))
                {
                    if (Utils.BuffMe("Blessing of Kings"))
                        return true;
                }
            }
            return false;
        }
        public override void talentsChanged()
        {
            judgementRange = 10;
            canCleanseRoot = false;
        }
        public override bool PullSpell()
        {
            if (getCurrentSpec().Equals(talentTrees[0], StringComparison.OrdinalIgnoreCase) || getCurrentSpec().Equals(talentTrees[3], StringComparison.OrdinalIgnoreCase))
            {
                if (retributionPullSpell())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[1], StringComparison.OrdinalIgnoreCase))
            {
                if (holyPullSpell())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[2], StringComparison.OrdinalIgnoreCase))
            {
                if (protectionPullSpell())
                    return true;
            }
            return false;
        }
        public override bool Heal()
        {
            if (getCurrentSpec().Equals(talentTrees[0], StringComparison.OrdinalIgnoreCase) || getCurrentSpec().Equals(talentTrees[3], StringComparison.OrdinalIgnoreCase))
            {
                if (retributionHeal())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[1], StringComparison.OrdinalIgnoreCase))
            {
                if (holyHeal())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[2], StringComparison.OrdinalIgnoreCase))
            {
                if (protectionHeal())
                    return true;
            }
            return false;
        }
        public override bool Buff()
        {
            if (classBuff())
                return true;
            else if (getCurrentSpec().Equals(talentTrees[0], StringComparison.OrdinalIgnoreCase) || getCurrentSpec().Equals(talentTrees[3], StringComparison.OrdinalIgnoreCase))
            {
                if (retributionBuff())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[1], StringComparison.OrdinalIgnoreCase))
            {
                if (holyBuff())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[2], StringComparison.OrdinalIgnoreCase))
            {
                if (protectionBuff())
                    return true;
            }

            return false;
        }
        public override bool Combat()
        {
            if (getCurrentSpec().Equals(talentTrees[0], StringComparison.OrdinalIgnoreCase) || getCurrentSpec().Equals(talentTrees[3], StringComparison.OrdinalIgnoreCase))
            {
                if (retributionCombat())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[1], StringComparison.OrdinalIgnoreCase))
            {
                if (holyCombat())
                    return true;
            }
            else if (getCurrentSpec().Equals(talentTrees[2], StringComparison.OrdinalIgnoreCase))
            {
                if (protectionCombat())
                    return true;
            }
            return false;
        }
        public override void checkGlyphs()
        {
            WoWSpell spell;
            List<String> glyphInfo;

            for (int i = 1; i <= 9; i++)
            {
                glyphInfo = getGlyph(i);
                spell = WoWSpell.FromId(glyphInfo.ElementAt(0).ToInt32());
                if (spell != null && !spell.Equals(""))
                    Utils.Log("Glyph found that modifies Spell: " + spell.Name);
            }
        }
        public override void checkTalents()
        {
            string name;
            int rank;
            List<String> talentInfo;

            //Enlightened Judgements
            talentInfo = getTalent(1, 11);
            name = talentInfo.ElementAt(0);
            rank = talentInfo.ElementAt(1).ToInt32();
            if (rank == 1)
            {
                Utils.Log(name + " found with " + rank + " point. Increasing Judgement range by 5 yards.");
                judgementRange += 5;
            }
            else if (rank == 2)
            {
                Utils.Log(name + " found with " + rank + " points. Increasing Judgement range by 10 yards.");
                judgementRange += 10;
            }

            //Sacred Cleansing
            talentInfo = getTalent(1, 14);
            name = talentInfo.ElementAt(0);
            rank = talentInfo.ElementAt(1).ToInt32();
            if (rank == 1)
            {
                Utils.Log(name + " found with " + rank + " point. Can dispel 1 Magic Effect.");
            }


            //Improved Judgement
            talentInfo = getTalent(3, 3);
            name = talentInfo.ElementAt(0);
            rank = talentInfo.ElementAt(1).ToInt32();
            if (rank == 1)
            {
                Utils.Log(name + " found with " + rank + " point. Increasing Judgement range by 10 yards.");
                judgementRange += 10;
            }
            else if (rank == 2)
            {
                Utils.Log(name + " found with " + rank + " points. Increasing Judgement range by 20 yards.");
                judgementRange += 20;
            }

            //Acts of Sacrifice
            talentInfo = getTalent(3, 19);
            name = talentInfo.ElementAt(0);
            rank = talentInfo.ElementAt(1).ToInt32();
            if (rank > 0)
            {
                Utils.Log(name + " found with " + rank + " point. Can cleanse movement impairing effects.");
                canCleanseRoot = true;
            }
        }
        public override string findSpec(int i)
        {
            return talentTrees[i];
        }
    }
}
